import numpy as np
np.random.seed(1337)  # for reproducibility 重复性
from keras.datasets import mnist
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers import Dense, Activation
from keras.optimizers import RMSprop



path='C:\\Users\\xxxx\\.keras\\datasets\\mnist.npz'
f = np.load(path)
X_train, Y_train = f['x_train'], f['y_train']
X_test, Y_test = f['x_test'], f['y_test']
f.close()

# download the mnist to the path '~/.keras/datasets/' if it is the first time to be called
# X shape (60,000 28x28), y shape (10,000, )
# (X_train, y_train), (X_test, y_test) = mnist.load_data()

# data pre-processing
X_train = X_train.reshape(X_train.shape[0], -1) / 255.   # normalize
X_test = X_test.reshape(X_test.shape[0], -1) / 255.      # normalize
# one hot
y_train = np_utils.to_categorical(Y_train, num_classes=10)
y_test = np_utils.to_categorical(Y_test, num_classes=10)

print(X_train[0].shape)
"""
(784,)
"""

print(y_train[:3])
"""
[[ 0.  0.  0.  0.  0.  1.  0.  0.  0.  0.]
 [ 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  1.  0.  0.  0.  0.  0.]]
"""
# Another way to build your neural net
model = Sequential([
    Dense(32, input_dim=784),
    Activation('relu'),
    Dense(10),
    Activation('softmax'),
])

# Another way to define your optimizer
rmsprop = RMSprop(lr=0.001, rho=0.9, epsilon=1e-08, decay=0.0)

# We add metrics to get more results you want to see
model.compile(optimizer=rmsprop,
              loss='categorical_crossentropy',
              metrics=['accuracy'])

print('Training ------------')
# Another way to train the model
model.fit(X_train, y_train, epochs=2, batch_size=32)

print('\nTesting ------------')
# Evaluate the model with the metrics we defined earlier
loss, accuracy = model.evaluate(X_test, y_test)

print('test loss: ', loss)
print('test accuracy: ', accuracy)

"""
Training ------------
D:/pyprojects/keras/Classifier/Classifier.py:57: UserWarning: The `nb_epoch` argument in `fit` has been renamed `epochs`.
  model.fit(X_train, y_train, nb_epoch=2, batch_size=32)
Epoch 1/2

   32/60000 [..............................] - ETA: 1:43:19 - loss: 2.4464 - acc: 0.0625
  608/60000 [..............................] - ETA: 5:28 - loss: 1.9224 - acc: 0.4145   
 1184/60000 [..............................] - ETA: 2:49 - loss: 1.6755 - acc: 0.5372
 1728/60000 [..............................] - ETA: 1:56 - loss: 1.5010 - acc: 0.6030
 2304/60000 [>.............................] - ETA: 1:28 - loss: 1.3378 - acc: 0.6567
 2880/60000 [>.............................] - ETA: 1:10 - loss: 1.2238 - acc: 0.6896
 3488/60000 [>.............................] - ETA: 58s - loss: 1.1222 - acc: 0.7167 
 4064/60000 [=>............................] - ETA: 50s - loss: 1.0453 - acc: 0.7367
 4672/60000 [=>............................] - ETA: 44s - loss: 0.9722 - acc: 0.7554
 5280/60000 [=>............................] - ETA: 39s - loss: 0.9230 - acc: 0.7659
 5920/60000 [=>............................] - ETA: 34s - loss: 0.8809 - acc: 0.7774
 6528/60000 [==>...........................] - ETA: 31s - loss: 0.8399 - acc: 0.7888
 7136/60000 [==>...........................] - ETA: 29s - loss: 0.8039 - acc: 0.7979
 7680/60000 [==>...........................] - ETA: 27s - loss: 0.7746 - acc: 0.8057
 8256/60000 [===>..........................] - ETA: 25s - loss: 0.7506 - acc: 0.8110
 8864/60000 [===>..........................] - ETA: 23s - loss: 0.7230 - acc: 0.8175
 9472/60000 [===>..........................] - ETA: 22s - loss: 0.7013 - acc: 0.8226
10048/60000 [====>.........................] - ETA: 20s - loss: 0.6845 - acc: 0.8255
10624/60000 [====>.........................] - ETA: 19s - loss: 0.6677 - acc: 0.8290
11200/60000 [====>.........................] - ETA: 18s - loss: 0.6538 - acc: 0.8315
11808/60000 [====>.........................] - ETA: 17s - loss: 0.6376 - acc: 0.8347
12416/60000 [=====>........................] - ETA: 16s - loss: 0.6233 - acc: 0.8386
13024/60000 [=====>........................] - ETA: 16s - loss: 0.6094 - acc: 0.8414
13632/60000 [=====>........................] - ETA: 15s - loss: 0.5978 - acc: 0.8435
14208/60000 [======>.......................] - ETA: 14s - loss: 0.5868 - acc: 0.8464
14816/60000 [======>.......................] - ETA: 14s - loss: 0.5772 - acc: 0.8487
15456/60000 [======>.......................] - ETA: 13s - loss: 0.5688 - acc: 0.8502
16064/60000 [=======>......................] - ETA: 12s - loss: 0.5596 - acc: 0.8528
16672/60000 [=======>......................] - ETA: 12s - loss: 0.5512 - acc: 0.8546
17248/60000 [=======>......................] - ETA: 11s - loss: 0.5433 - acc: 0.8563
17824/60000 [=======>......................] - ETA: 11s - loss: 0.5339 - acc: 0.8587
18400/60000 [========>.....................] - ETA: 11s - loss: 0.5264 - acc: 0.8604
18976/60000 [========>.....................] - ETA: 10s - loss: 0.5208 - acc: 0.8617
19584/60000 [========>.....................] - ETA: 10s - loss: 0.5140 - acc: 0.8630
20192/60000 [=========>....................] - ETA: 9s - loss: 0.5067 - acc: 0.8651 
20800/60000 [=========>....................] - ETA: 9s - loss: 0.5007 - acc: 0.8665
21376/60000 [=========>....................] - ETA: 9s - loss: 0.4960 - acc: 0.8677
21952/60000 [=========>....................] - ETA: 9s - loss: 0.4929 - acc: 0.8687
22528/60000 [==========>...................] - ETA: 8s - loss: 0.4883 - acc: 0.8695
23104/60000 [==========>...................] - ETA: 8s - loss: 0.4839 - acc: 0.8705
23680/60000 [==========>...................] - ETA: 8s - loss: 0.4796 - acc: 0.8713
24256/60000 [===========>..................] - ETA: 8s - loss: 0.4760 - acc: 0.8722
24832/60000 [===========>..................] - ETA: 7s - loss: 0.4709 - acc: 0.8733
25408/60000 [===========>..................] - ETA: 7s - loss: 0.4672 - acc: 0.8741
25984/60000 [===========>..................] - ETA: 7s - loss: 0.4631 - acc: 0.8752
26592/60000 [============>.................] - ETA: 7s - loss: 0.4600 - acc: 0.8756
27232/60000 [============>.................] - ETA: 6s - loss: 0.4555 - acc: 0.8766
27808/60000 [============>.................] - ETA: 6s - loss: 0.4521 - acc: 0.8773
28416/60000 [=============>................] - ETA: 6s - loss: 0.4486 - acc: 0.8782
28992/60000 [=============>................] - ETA: 6s - loss: 0.4464 - acc: 0.8786
29568/60000 [=============>................] - ETA: 6s - loss: 0.4433 - acc: 0.8793
30144/60000 [==============>...............] - ETA: 5s - loss: 0.4389 - acc: 0.8804
30720/60000 [==============>...............] - ETA: 5s - loss: 0.4354 - acc: 0.8812
31264/60000 [==============>...............] - ETA: 5s - loss: 0.4323 - acc: 0.8820
31872/60000 [==============>...............] - ETA: 5s - loss: 0.4298 - acc: 0.8828
32448/60000 [===============>..............] - ETA: 5s - loss: 0.4279 - acc: 0.8833
33024/60000 [===============>..............] - ETA: 5s - loss: 0.4245 - acc: 0.8841
33536/60000 [===============>..............] - ETA: 4s - loss: 0.4212 - acc: 0.8847
34112/60000 [================>.............] - ETA: 4s - loss: 0.4191 - acc: 0.8851
34720/60000 [================>.............] - ETA: 4s - loss: 0.4175 - acc: 0.8853
35296/60000 [================>.............] - ETA: 4s - loss: 0.4157 - acc: 0.8856
35840/60000 [================>.............] - ETA: 4s - loss: 0.4128 - acc: 0.8864
36448/60000 [=================>............] - ETA: 4s - loss: 0.4104 - acc: 0.8871
37056/60000 [=================>............] - ETA: 4s - loss: 0.4079 - acc: 0.8876
37696/60000 [=================>............] - ETA: 3s - loss: 0.4062 - acc: 0.8880
38304/60000 [==================>...........] - ETA: 3s - loss: 0.4036 - acc: 0.8886
38944/60000 [==================>...........] - ETA: 3s - loss: 0.4019 - acc: 0.8891
39552/60000 [==================>...........] - ETA: 3s - loss: 0.4000 - acc: 0.8894
40160/60000 [===================>..........] - ETA: 3s - loss: 0.3982 - acc: 0.8899
40800/60000 [===================>..........] - ETA: 3s - loss: 0.3964 - acc: 0.8904
41408/60000 [===================>..........] - ETA: 3s - loss: 0.3940 - acc: 0.8911
41984/60000 [===================>..........] - ETA: 2s - loss: 0.3929 - acc: 0.8915
42624/60000 [====================>.........] - ETA: 2s - loss: 0.3907 - acc: 0.8920
43232/60000 [====================>.........] - ETA: 2s - loss: 0.3885 - acc: 0.8925
43840/60000 [====================>.........] - ETA: 2s - loss: 0.3866 - acc: 0.8931
44416/60000 [=====================>........] - ETA: 2s - loss: 0.3861 - acc: 0.8932
44960/60000 [=====================>........] - ETA: 2s - loss: 0.3839 - acc: 0.8939
45600/60000 [=====================>........] - ETA: 2s - loss: 0.3815 - acc: 0.8946
46240/60000 [======================>.......] - ETA: 2s - loss: 0.3796 - acc: 0.8951
46848/60000 [======================>.......] - ETA: 2s - loss: 0.3777 - acc: 0.8955
47456/60000 [======================>.......] - ETA: 1s - loss: 0.3758 - acc: 0.8960
48032/60000 [=======================>......] - ETA: 1s - loss: 0.3737 - acc: 0.8965
48608/60000 [=======================>......] - ETA: 1s - loss: 0.3725 - acc: 0.8967
49184/60000 [=======================>......] - ETA: 1s - loss: 0.3710 - acc: 0.8971
49792/60000 [=======================>......] - ETA: 1s - loss: 0.3688 - acc: 0.8977
50400/60000 [========================>.....] - ETA: 1s - loss: 0.3676 - acc: 0.8981
51040/60000 [========================>.....] - ETA: 1s - loss: 0.3662 - acc: 0.8985
51680/60000 [========================>.....] - ETA: 1s - loss: 0.3645 - acc: 0.8990
52256/60000 [=========================>....] - ETA: 1s - loss: 0.3627 - acc: 0.8995
52832/60000 [=========================>....] - ETA: 1s - loss: 0.3612 - acc: 0.8999
53440/60000 [=========================>....] - ETA: 0s - loss: 0.3598 - acc: 0.9004
54016/60000 [==========================>...] - ETA: 0s - loss: 0.3584 - acc: 0.9006
54624/60000 [==========================>...] - ETA: 0s - loss: 0.3567 - acc: 0.9010
55232/60000 [==========================>...] - ETA: 0s - loss: 0.3551 - acc: 0.9016
55808/60000 [==========================>...] - ETA: 0s - loss: 0.3535 - acc: 0.9019
56384/60000 [===========================>..] - ETA: 0s - loss: 0.3522 - acc: 0.9023
56960/60000 [===========================>..] - ETA: 0s - loss: 0.3506 - acc: 0.9027
57568/60000 [===========================>..] - ETA: 0s - loss: 0.3495 - acc: 0.9030
58176/60000 [============================>.] - ETA: 0s - loss: 0.3480 - acc: 0.9034
58720/60000 [============================>.] - ETA: 0s - loss: 0.3463 - acc: 0.9038
59296/60000 [============================>.] - ETA: 0s - loss: 0.3449 - acc: 0.9042
59904/60000 [============================>.] - ETA: 0s - loss: 0.3435 - acc: 0.9046
60000/60000 [==============================] - 9s 143us/step - loss: 0.3434 - acc: 0.9046
Epoch 2/2

   32/60000 [..............................] - ETA: 11s - loss: 0.0645 - acc: 1.0000
  640/60000 [..............................] - ETA: 5s - loss: 0.2140 - acc: 0.9375 
 1216/60000 [..............................] - ETA: 5s - loss: 0.2166 - acc: 0.9383
 1792/60000 [..............................] - ETA: 5s - loss: 0.2273 - acc: 0.9369
 2368/60000 [>.............................] - ETA: 5s - loss: 0.2315 - acc: 0.9367
 2944/60000 [>.............................] - ETA: 5s - loss: 0.2355 - acc: 0.9372
 3520/60000 [>.............................] - ETA: 5s - loss: 0.2300 - acc: 0.9375
 4160/60000 [=>............................] - ETA: 4s - loss: 0.2253 - acc: 0.9370
 4768/60000 [=>............................] - ETA: 4s - loss: 0.2209 - acc: 0.9369
 5376/60000 [=>............................] - ETA: 4s - loss: 0.2198 - acc: 0.9373
 5952/60000 [=>............................] - ETA: 4s - loss: 0.2176 - acc: 0.9373
 6560/60000 [==>...........................] - ETA: 4s - loss: 0.2197 - acc: 0.9375
 7200/60000 [==>...........................] - ETA: 4s - loss: 0.2214 - acc: 0.9357
 7808/60000 [==>...........................] - ETA: 4s - loss: 0.2213 - acc: 0.9357
 8320/60000 [===>..........................] - ETA: 4s - loss: 0.2242 - acc: 0.9356
 8896/60000 [===>..........................] - ETA: 4s - loss: 0.2226 - acc: 0.9357
 9472/60000 [===>..........................] - ETA: 4s - loss: 0.2200 - acc: 0.9360
10080/60000 [====>.........................] - ETA: 4s - loss: 0.2210 - acc: 0.9357
10624/60000 [====>.........................] - ETA: 4s - loss: 0.2190 - acc: 0.9365
11200/60000 [====>.........................] - ETA: 4s - loss: 0.2180 - acc: 0.9373
11808/60000 [====>.........................] - ETA: 4s - loss: 0.2155 - acc: 0.9380
12416/60000 [=====>........................] - ETA: 4s - loss: 0.2169 - acc: 0.9382
12992/60000 [=====>........................] - ETA: 4s - loss: 0.2158 - acc: 0.9389
13600/60000 [=====>........................] - ETA: 4s - loss: 0.2165 - acc: 0.9385
14208/60000 [======>.......................] - ETA: 4s - loss: 0.2159 - acc: 0.9386
14848/60000 [======>.......................] - ETA: 3s - loss: 0.2156 - acc: 0.9386
15488/60000 [======>.......................] - ETA: 3s - loss: 0.2139 - acc: 0.9394
16064/60000 [=======>......................] - ETA: 3s - loss: 0.2122 - acc: 0.9399
16640/60000 [=======>......................] - ETA: 3s - loss: 0.2100 - acc: 0.9404
17248/60000 [=======>......................] - ETA: 3s - loss: 0.2094 - acc: 0.9406
17856/60000 [=======>......................] - ETA: 3s - loss: 0.2088 - acc: 0.9410
18464/60000 [========>.....................] - ETA: 3s - loss: 0.2090 - acc: 0.9412
19040/60000 [========>.....................] - ETA: 3s - loss: 0.2088 - acc: 0.9412
19648/60000 [========>.....................] - ETA: 3s - loss: 0.2074 - acc: 0.9416
20192/60000 [=========>....................] - ETA: 3s - loss: 0.2068 - acc: 0.9417
20768/60000 [=========>....................] - ETA: 3s - loss: 0.2065 - acc: 0.9418
21344/60000 [=========>....................] - ETA: 3s - loss: 0.2063 - acc: 0.9417
21888/60000 [=========>....................] - ETA: 3s - loss: 0.2069 - acc: 0.9415
22464/60000 [==========>...................] - ETA: 3s - loss: 0.2062 - acc: 0.9418
23040/60000 [==========>...................] - ETA: 3s - loss: 0.2058 - acc: 0.9419
23648/60000 [==========>...................] - ETA: 3s - loss: 0.2063 - acc: 0.9416
24288/60000 [===========>..................] - ETA: 3s - loss: 0.2046 - acc: 0.9420
24896/60000 [===========>..................] - ETA: 3s - loss: 0.2042 - acc: 0.9420
25536/60000 [===========>..................] - ETA: 3s - loss: 0.2030 - acc: 0.9424
26144/60000 [============>.................] - ETA: 2s - loss: 0.2028 - acc: 0.9422
26752/60000 [============>.................] - ETA: 2s - loss: 0.2039 - acc: 0.9418
27328/60000 [============>.................] - ETA: 2s - loss: 0.2050 - acc: 0.9415
27904/60000 [============>.................] - ETA: 2s - loss: 0.2051 - acc: 0.9414
28448/60000 [=============>................] - ETA: 2s - loss: 0.2042 - acc: 0.9416
29056/60000 [=============>................] - ETA: 2s - loss: 0.2050 - acc: 0.9416
29664/60000 [=============>................] - ETA: 2s - loss: 0.2053 - acc: 0.9414
30304/60000 [==============>...............] - ETA: 2s - loss: 0.2049 - acc: 0.9416
30912/60000 [==============>...............] - ETA: 2s - loss: 0.2047 - acc: 0.9416
31456/60000 [==============>...............] - ETA: 2s - loss: 0.2042 - acc: 0.9417
31968/60000 [==============>...............] - ETA: 2s - loss: 0.2044 - acc: 0.9417
32512/60000 [===============>..............] - ETA: 2s - loss: 0.2042 - acc: 0.9417
33056/60000 [===============>..............] - ETA: 2s - loss: 0.2037 - acc: 0.9419
33664/60000 [===============>..............] - ETA: 2s - loss: 0.2035 - acc: 0.9420
34240/60000 [================>.............] - ETA: 2s - loss: 0.2031 - acc: 0.9421
34816/60000 [================>.............] - ETA: 2s - loss: 0.2033 - acc: 0.9421
35392/60000 [================>.............] - ETA: 2s - loss: 0.2029 - acc: 0.9420
35968/60000 [================>.............] - ETA: 2s - loss: 0.2025 - acc: 0.9420
36576/60000 [=================>............] - ETA: 2s - loss: 0.2025 - acc: 0.9420
37184/60000 [=================>............] - ETA: 2s - loss: 0.2024 - acc: 0.9420
37792/60000 [=================>............] - ETA: 1s - loss: 0.2014 - acc: 0.9422
38336/60000 [==================>...........] - ETA: 1s - loss: 0.2014 - acc: 0.9421
38880/60000 [==================>...........] - ETA: 1s - loss: 0.2006 - acc: 0.9423
39424/60000 [==================>...........] - ETA: 1s - loss: 0.1997 - acc: 0.9426
39840/60000 [==================>...........] - ETA: 1s - loss: 0.1994 - acc: 0.9426
40384/60000 [===================>..........] - ETA: 1s - loss: 0.1985 - acc: 0.9428
40960/60000 [===================>..........] - ETA: 1s - loss: 0.1984 - acc: 0.9429
41536/60000 [===================>..........] - ETA: 1s - loss: 0.1983 - acc: 0.9431
42112/60000 [====================>.........] - ETA: 1s - loss: 0.1982 - acc: 0.9431
42656/60000 [====================>.........] - ETA: 1s - loss: 0.1977 - acc: 0.9431
43168/60000 [====================>.........] - ETA: 1s - loss: 0.1976 - acc: 0.9431
43648/60000 [====================>.........] - ETA: 1s - loss: 0.1973 - acc: 0.9433
44256/60000 [=====================>........] - ETA: 1s - loss: 0.1973 - acc: 0.9434
44832/60000 [=====================>........] - ETA: 1s - loss: 0.1974 - acc: 0.9433
45408/60000 [=====================>........] - ETA: 1s - loss: 0.1971 - acc: 0.9434
45984/60000 [=====================>........] - ETA: 1s - loss: 0.1973 - acc: 0.9434
46560/60000 [======================>.......] - ETA: 1s - loss: 0.1968 - acc: 0.9435
47104/60000 [======================>.......] - ETA: 1s - loss: 0.1968 - acc: 0.9435
47680/60000 [======================>.......] - ETA: 1s - loss: 0.1964 - acc: 0.9436
48320/60000 [=======================>......] - ETA: 1s - loss: 0.1966 - acc: 0.9435
48928/60000 [=======================>......] - ETA: 0s - loss: 0.1963 - acc: 0.9434
49504/60000 [=======================>......] - ETA: 0s - loss: 0.1959 - acc: 0.9436
50112/60000 [========================>.....] - ETA: 0s - loss: 0.1966 - acc: 0.9436
50720/60000 [========================>.....] - ETA: 0s - loss: 0.1969 - acc: 0.9434
51296/60000 [========================>.....] - ETA: 0s - loss: 0.1964 - acc: 0.9436
51840/60000 [========================>.....] - ETA: 0s - loss: 0.1961 - acc: 0.9437
52352/60000 [=========================>....] - ETA: 0s - loss: 0.1959 - acc: 0.9437
52896/60000 [=========================>....] - ETA: 0s - loss: 0.1961 - acc: 0.9435
53440/60000 [=========================>....] - ETA: 0s - loss: 0.1960 - acc: 0.9435
53984/60000 [=========================>....] - ETA: 0s - loss: 0.1962 - acc: 0.9434
54528/60000 [==========================>...] - ETA: 0s - loss: 0.1966 - acc: 0.9433
55104/60000 [==========================>...] - ETA: 0s - loss: 0.1962 - acc: 0.9433
55648/60000 [==========================>...] - ETA: 0s - loss: 0.1960 - acc: 0.9434
56256/60000 [===========================>..] - ETA: 0s - loss: 0.1963 - acc: 0.9434
56864/60000 [===========================>..] - ETA: 0s - loss: 0.1962 - acc: 0.9435
57504/60000 [===========================>..] - ETA: 0s - loss: 0.1956 - acc: 0.9436
58080/60000 [============================>.] - ETA: 0s - loss: 0.1954 - acc: 0.9436
58688/60000 [============================>.] - ETA: 0s - loss: 0.1950 - acc: 0.9438
59328/60000 [============================>.] - ETA: 0s - loss: 0.1948 - acc: 0.9438
59904/60000 [============================>.] - ETA: 0s - loss: 0.1947 - acc: 0.9438
60000/60000 [==============================] - 5s 89us/step - loss: 0.1946 - acc: 0.9438

Testing ------------

   32/10000 [..............................] - ETA: 2:57
 1472/10000 [===>..........................] - ETA: 3s  
 2944/10000 [=======>......................] - ETA: 1s
 4384/10000 [============>.................] - ETA: 0s
 5792/10000 [================>.............] - ETA: 0s
 7264/10000 [====================>.........] - ETA: 0s
 8704/10000 [=========================>....] - ETA: 0s
10000/10000 [==============================] - 1s 92us/step
test loss:  0.17416930682733656
test accuracy:  0.9502

"""
